#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define MAX 10

/**< hat colors */
#define WHAT 0
#define BHAT 1

const int hatcolor[2] = {
    WHAT, BHAT,
};

int main(void)
{
    int hats[MAX];
    int i = 0, j = 0;
    int total_live = 0;

    /**< someone who knows the odd of white hats */
    int white_hat_odd[MAX];

    /**< answer `white`@0 or `black`@1 */
    int answers[MAX];

    srandom(getpid());
    for (i = 0; i < MAX; i++) 
        hats[i] = hatcolor[random()%2];

    /**< odd of white hats */
    int white_hats = 0;    
    white_hat_odd[0] = 0;
    for (i = 1; i < MAX; i++) {
        if (hats[i-1] == WHAT) {
            white_hats++;
            white_hat_odd[i] = white_hats%2;
        } else {
            white_hat_odd[i] = white_hat_odd[i-1];
        }
    }

    /**< get answers */
    answers[MAX-1] = (white_hat_odd[MAX-1] == 1)?1:0;
    for (i = MAX-2; i>0; i--) {
        answers[i] = (white_hat_odd[i] == white_hat_odd[i+1])?1:0;
    }
    answers[0] = (white_hat_odd[1] == 1)?0:1;

    printf("\n"
           "@Notes\n"
           "`0` stands for white hat;\n"
           "`1` stands for black hat."
           "\n\n"
        );

    /**< print the 10 persons hat's color */
    printf("%-10s", "Person");
    for (i = MAX; i > 0; i--)
        printf("%2d  ", i);
    printf("\n-------------------------------------------------\n");

    /**< print hats' colors */
    printf("%-10s", "Color");
    for (i = MAX; i > 0; i--)
        printf("%2d  ", hats[i-1]);
    printf("\n\n");
    
    /**< print white hats odd */
    printf("%-10s", "`0` odd");
    for (i = MAX; i > 0; i--)
        printf("%2d  ", white_hat_odd[i-1]);
    printf("\n\n");
    
    /**< print the answers */
    printf("%-10s", "answers");
    for (i = MAX; i > 0; i--)
        printf("%2d  ", answers[i-1]);
    printf("\n\n");
    
    for (i = 0; i < MAX; i++)
        if (answers[i] == hats[i]) total_live++;
    printf("Total live:%d\n", total_live);
    printf("\n-------------------------------------------------\n\n");

    return 0;
}